package com.vsd.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.domain.Code;
import com.demo.util.JSONUtils;
import com.demo.validator.AdminLoginValidate;
import com.vsd.service.CodeService;

@Controller
@RequestMapping("code")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class CodeController extends CoreAdminController{
	@Autowired
	private CodeService codeService;
 
	
	@RequestMapping("/list")
	@ResponseBody
	public String citylist(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,HttpServletRequest request){
		
		Page<Code> list = codeService.findAll(new PageRequest(start/length, length, new Sort(Direction.DESC, "code")));
		
		return JSONUtils.serialize(new DataTable(draw+1,Integer.parseInt(list.getTotalElements()+""), list.getContent()));
	}
	
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(Code code){
		codeService.updateByPrimaryKeySelective(code);
		return JSONUtils.serialize(new Ret(0));
	}
	

	@RequestMapping("/findById")
	@ResponseBody
	public String findById(String id){
		Code c = codeService.findOne(id);
		return JSONUtils.serialize(c);
	}
	
	 
	
}
